﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using SmartSpace.UI.Silverlight.Dynamic.Utils;

namespace SmartSpace.UI.Silverlight.Dynamic
{
    public partial class ListTemplateView : AppView
    {
        private Guid _workflowId;
        private Guid _activityId;

        public Guid WorkflowId { get { return _workflowId; } }
        public Guid ActivityId { get { return _activityId; } }

        public ListTemplateView()
            :base(new Guid("{3E7C1764-83A5-4828-9C60-41A9DCA8277A}"), "列表数据模版")
        {
            InitializeComponent();

            this.m_dataGrid.DataContext = this;
        }

        protected override void OnExtendShown()
        {
            if (IsFirstDisplay)
            {
                // 加载数据
                _workflowId = new Guid(Arguments["workflowid"].ToString());
                _activityId = new Guid(Arguments["activityid"].ToString());
                LoadMetadata();
            }
        }

        private void LoadMetadata()
        {
            m_busy.IsBusy = true;

            DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
            client.GetViewMetadataCompleted += new EventHandler<GetViewMetadataCompletedEventArgs>(client_GetViewMetadataCompleted);
            client.GetViewMetadataAsync(_workflowId, _activityId, 0, 0, this.Authentication.UserID);
        }

        void client_GetViewMetadataCompleted(object sender, GetViewMetadataCompletedEventArgs e)
        {
            if (e.Error != null)
            {
            }
            else if (!e.Result.IsSuccess)
            {
            }
            else
            {
                ListTemplateUtil.Build(m_dataGrid, e.Result.Data);

                // 加载数据
                LoadData();
            }
        }

        private void LoadData()
        {
            DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
            client.GetListDataCompleted += new EventHandler<GetListDataCompletedEventArgs>(client_GetListDataCompleted);
            client.GetListDataAsync(_workflowId, _activityId, 0, this.Authentication.UserID, 0, 25);
        }

        void client_GetListDataCompleted(object sender, GetListDataCompletedEventArgs e)
        {
            if (e.Error != null)
            {
            }
            else if (!e.Result.IsSuccess)
            {
            }
            else
            {
                m_dataGrid.ItemsSource = e.Result.Data;
                m_busy.IsBusy = false;
            }
        }

        private void m_btnView_Click(object sender, RoutedEventArgs e)
        {

        }

        private void m_btnRefresh_Click(object sender, RoutedEventArgs e)
        {

        }

        public void ShowDetail(DataInstance data)
        {
            DetailTemplateView detailView = new DetailTemplateView(_workflowId, _activityId, data.Id);
            detailView.Hide += new ViewHideEventHandler(detailView_Hide);
            this.SwitchView(detailView);
        }

        void detailView_Hide(SlView sender, bool isCancel)
        {
            if (!isCancel)
            {
                LoadData();
            }
        }
    }
}
